package conson.tech.manage.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import conson.tech.manage.api.app.constant.AppCommon;
import conson.tech.manage.api.app.dto.AppConfigDTO;
import conson.tech.manage.api.app.dto.AppGroupConfigDTO;
import conson.tech.manage.app.entity.pojo.AppConfig;
import conson.tech.manage.app.entity.pojo.AppGroupConfig;
import conson.tech.manage.app.entity.vo.*;
import conson.tech.manage.app.mapper.AppGroupConfigMapper;
import conson.tech.manage.app.service.AppConfigService;
import conson.tech.manage.app.service.AppGroupConfigService;
import conson.tech.manage.common.util.EntityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: AppGroupConfigServiceImpl
 * Description:
 * Author: HXL
 * Date: 2020/4/23 2:58 下午
 * Version: 1.0
 */
@Service("appGroupConfigService")
public class AppGroupConfigServiceImpl implements AppGroupConfigService {

    @Autowired
    private AppGroupConfigMapper appGroupConfigMapper;
    @Autowired
    private AppConfigService appConfigService;
    @Override
    @Transactional
    public int appAddGroup(AppGroupConfig groupConfig) {
        groupConfig.setCreatedTime(LocalDateTime.now());
        groupConfig.setUpdatedTime(LocalDateTime.now());
        return appGroupConfigMapper.insert(groupConfig);
    }

    @Override
    @Transactional
    public int appUpdateGroup(AppGroupConfig groupConfig) {
        groupConfig.setUpdatedTime(LocalDateTime.now());
        return appGroupConfigMapper.updateById(groupConfig);
    }

    @Override
    @Transactional
    public int appDeleteGroup(AppGroupConfig groupConfig) {
        groupConfig.setUpdatedTime(LocalDateTime.now());
        return appGroupConfigMapper.updateById(groupConfig);
    }
    /**
     * Title: 应用分组列表查询（应用分类配置列表）
     * Description:
     * @param vo
     * @return: com.baomidou.mybatisplus.extension.plugins.pagination.Page<conson.tech.manage.app.entity.vo.AppGroupConfigVo>
     * Company: GX
     * Author HXL
     * Date 2020/4/24 2:28 下午
     */
    @Override
    public Page<AppClassVo> queryGroupListPage(PageVo vo) {
        Page<AppGroupConfig> page = new Page();
        page.setCurrent(vo.getPageIndex());
        page.setSize(vo.getPageSize());
        Page<AppGroupConfig> groupConfigPage = appGroupConfigMapper.queryGroupPage(page);

        Page<AppClassVo> appClassVoPage = new Page<>();
        if (groupConfigPage != null) {
            appClassVoPage = EntityUtils.pageToPage(groupConfigPage, AppClassVo.class);
        }
        if (!CollectionUtils.isEmpty(appClassVoPage.getRecords())){
            for (AppClassVo classVo : appClassVoPage.getRecords()) {
                if (classVo == null) continue;
                Integer num = appConfigService.selectAppNumByGroupId(classVo.getId());
                classVo.setAppNum(num == null?0:num);
            }
        }
        return appClassVoPage;
    }

    /**
     * Title: 根据集合插入分组数据
     * Description:
     * @param groupConfigList
     * @return: int
     * Company: GX
     * Author HXL
     * Date 2020/4/26 3:43 下午
     */
    @Override
    public int appAddGroupByList(List<AppGroupConfig> groupConfigList) {
        int result = 0;
        if (!CollectionUtils.isEmpty(groupConfigList)){
            for (AppGroupConfig groupConfig : groupConfigList) {
                groupConfig.setCreatedTime(LocalDateTime.now());
                groupConfig.setUpdatedTime(LocalDateTime.now());
                result +=  appGroupConfigMapper.insert(groupConfig);
            }
        }
        return result;
    }
    /**
     * Title: 根据主键ID修改
     * Description:
     * @param groupConfigList
     * @return: int
     * Company: GX
     * Author HXL
     * Date 2020/4/27 8:51 上午
     */
    @Transactional
    @Override
    public int appUpdateGroupByList(List<AppGroupConfig> groupConfigList) {
        int result = 0;
        if (!CollectionUtils.isEmpty(groupConfigList)){
            for (AppGroupConfig groupConfig : groupConfigList) {
                groupConfig.setUpdatedTime(LocalDateTime.now());
                result +=  appGroupConfigMapper.updateById(groupConfig);
            }
        }
        return result;
    }
    /**
     * Title:查询首页生活页所需要的分组
     * Description:
     * @param requestPageType
     * @return: java.util.List<conson.tech.manage.app.entity.vo.QueryAppGroupVo>
     * Company: GX
     * Author HXL
     * Date 2020/4/27 10:37 上午
     */
    @Override
    public List<QueryAppGroupVo> queryGroupList(Integer requestPageType) {
        List<AppGroupConfig> groupConfigList = appGroupConfigMapper.queryGroupList(requestPageType);
        return EntityUtils.listToList(groupConfigList, QueryAppGroupVo.class);
    }

    @Override
    public AppGroupConfig queryGroupByGroupId(Long groupId,Integer versionValue) {
        return appGroupConfigMapper.queryGroupByGroupId(groupId,versionValue);
    }

    @Override
    public Page<AppSpecialVo> querySpecial(PageVo pageVo) {
        Page<AppSpecialVo> page = new Page();
        page.setCurrent(pageVo.getPageIndex());
        page.setSize(pageVo.getPageSize());
        Page<AppSpecialVo> appSpecialVoPage = appGroupConfigMapper.querySpecial(page);
        if (appSpecialVoPage != null){
            List<AppSpecialVo> records = appSpecialVoPage.getRecords();
            if (!CollectionUtils.isEmpty(records)){
                for (AppSpecialVo record : records) {
                    QueryWrapper queryWrapper = new QueryWrapper();
                    AppConfig appConfig = new AppConfig();
                    appConfig.setDelFlag(AppCommon.APP_DEL_FLAG_ONE);
                    appConfig.setPlateType(AppCommon.APP_PLATE_TYPE_THREE);
                    appConfig.setGroupId(record.getId());
                    queryWrapper.setEntity(appConfig);
                    queryWrapper.orderByAsc("sort");
                   List<AppConfig> configList =  appConfigService.selectList(queryWrapper);
                   if (!CollectionUtils.isEmpty(configList)){
                       record.setSpecialSubVoList(EntityUtils.listToList(configList, AppSpecialSubVo.class));
                   }
                }
            }
        }
        return appSpecialVoPage;
    }

    @Override
    @Transactional
    public int addSpecial(AppSpecialVo appSpecialVo) {
        AppGroupConfig appGroupConfig = new AppGroupConfig();
        appGroupConfig.setName(appSpecialVo.getName());
        appGroupConfig.setOpen(1);
        appGroupConfig.setSort(1);
        appGroupConfig.setGroupType(3);
        appGroupConfig.setDelFlag(1);
        appGroupConfig.setCreatedTime(LocalDateTime.now());
        appGroupConfig.setUpdatedTime(LocalDateTime.now());
        int i =  appGroupConfigMapper.insertByAppGroup(appGroupConfig);
        if (!CollectionUtils.isEmpty(appSpecialVo.getSpecialSubVoList()) && i > 0){
            List<AppConfig> appConfigList = new ArrayList<>();
            for (AppSpecialSubVo appSpecialSubVo : appSpecialVo.getSpecialSubVoList()) {
                AppConfig appConfig = new AppConfig();
                BeanUtils.copyProperties(appSpecialSubVo,appConfig);
                appConfig.setDelFlag(1);
                appConfig.setEditFlag(1);
                appConfig.setDefaultIndex(0);
                appConfig.setGroupId(appGroupConfig.getId());
                appConfig.setPlateType(3);
                appConfigList.add(appConfig);
            }
            appConfigService.insertAppConfigByList(appConfigList);
        }
        return i;
    }

    @Override
    @Transactional
    public int updateSpecial(AppSpecialVo appSpecialVo) {
        AppGroupConfig appGroupConfig = new AppGroupConfig();
        appGroupConfig.setName(appSpecialVo.getName());
        appGroupConfig.setId(appSpecialVo.getId());
        appGroupConfig.setUpdatedTime(LocalDateTime.now());
        int i =  appGroupConfigMapper.updateById(appGroupConfig);
        QueryWrapper queryWrapper = new QueryWrapper();
        AppConfig appConfig1 = new AppConfig();
        appConfig1.setGroupId(appGroupConfig.getId());
        queryWrapper.setEntity(appConfig1);
        List<AppConfig> appConfigs = appConfigService.selectList(queryWrapper);

        if (!CollectionUtils.isEmpty(appSpecialVo.getSpecialSubVoList())){
            List<AppConfig> deleteList = new ArrayList<>();
            if (!CollectionUtils.isEmpty(appConfigs)){
                for (AppConfig appConfig : appConfigs) {
                    int count = 0;
                    for (AppSpecialSubVo appSpecialSubVo : appSpecialVo.getSpecialSubVoList()) {
                        if (appSpecialSubVo.getId() != null){
                            if (appConfig.getId().longValue() == appSpecialSubVo.getId().longValue()){
                                count ++;
                            }
                        }
                    }
                    if (count == 0){
                        deleteList.add(appConfig);
                    }
                }
            }
            List<AppConfig> appConfigList = new ArrayList<>();
            List<AppConfig> newAppConfigList = new ArrayList<>();
            for (AppSpecialSubVo appSpecialSubVo : appSpecialVo.getSpecialSubVoList()) {
                AppConfig appConfig = new AppConfig();
                BeanUtils.copyProperties(appSpecialSubVo,appConfig);
                if (appSpecialSubVo.getId()!= null) {
                    appConfigList.add(appConfig);
                }else {
                    appConfig.setEditFlag(1);
                    appConfig.setPlateType(3);
                    appConfig.setDefaultIndex(0);
                    appConfig.setAuthorize(1);
                    appConfig.setLogin(1);
                    appConfig.setOpen(1);
                    appConfig.setStartVersion(AppCommon.VERSION_NAME);
                    appConfig.setStartVersionValue(AppCommon.VERSION_VALUE);
                    appConfig.setEndVersion(AppCommon.VERSION_NAME);
                    appConfig.setEndVersionValue(AppCommon.VERSION_VALUE);
                    appConfig.setGroupId(appGroupConfig.getId());
                    newAppConfigList.add(appConfig);
                }
            }
            if (!CollectionUtils.isEmpty(deleteList)){
                for (AppConfig appConfig : deleteList) {
                    appConfigService.deleteAppConfig(appConfig.getId());
                }
            }
            if (!CollectionUtils.isEmpty(appConfigList)) {
                appConfigService.updateAppConfigByList(appConfigList);
            }
            if (!CollectionUtils.isEmpty(newAppConfigList)) {
                appConfigService.insertAppConfigByList(newAppConfigList);
            }
        }
        return i;
    }

    @Override
    @Transactional
    public int deleteSpecial(Long id) {
        AppGroupConfig appGroupConfig = new AppGroupConfig();
        appGroupConfig.setId(id);
        appGroupConfig.setDelFlag(0);
        appGroupConfig.setUpdatedTime(LocalDateTime.now());
        int i = appGroupConfigMapper.updateById(appGroupConfig);
        if (i > 0){
            appConfigService.deleteAppConfigByGroupId(id);
        }
        return i;
    }

    @Override
    public List<AppGroupConfigDTO> queryAppGroupByType(Integer type,Integer versionValue) {
        List<AppGroupConfig> list = appGroupConfigMapper.queryAppGroupByType(type, versionValue);
        List<AppGroupConfigDTO> groupConfigDTOList = EntityUtils.listToList(list, AppGroupConfigDTO.class);
        if (!CollectionUtils.isEmpty(groupConfigDTOList)){
            for (AppGroupConfigDTO appGroupConfigDTO : groupConfigDTOList) {
                appGroupConfigDTO.setAppConfigList(EntityUtils.listToList(appGroupConfigDTO.getAppConfigList(), AppConfigDTO.class));
            }
        }
        return  groupConfigDTOList;
    }
}
